﻿using System;
using System.Reflection;
using System.Windows;
using System.Windows.Navigation;
using Newtonsoft.Json.Linq;

namespace WindowsPhoneAppHost
{
    public class Common
    {
        public static void executePageMethod(string methodName, object sender, RoutedEventArgs e)
        {
            if (null != methodName && methodName.Length > 0)
            {
                var page = (Application.Current.RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).Content as Microsoft.Phone.Controls.PhoneApplicationPage;
                var type = page.GetType();
                //MethodInfo[] myMethodInfo = type.GetMethods();
                //MethodInfo methodInfo = type.GetMethod("button1_Click_1", BindingFlags.NonPublic | BindingFlags.Instance);
                MethodInfo methodInfo = type.GetMethod(methodName);
                //methodInfo = type.GetMethod("ShowAlert");
                //MethodInfo methodInfo = page.GetType().GetMethod(ButtonClick);
                methodInfo.Invoke(page, new object[] { sender, e });
            }
        }

        public static void navigateTo(string xamlPageName)
        {
            var page = (Application.Current.RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).Content as Microsoft.Phone.Controls.PhoneApplicationPage;
            page.NavigationService.Navigate(new Uri(xamlPageName, UriKind.Relative));
        }

        //JObject.TryGetValue ??
        public static string getJsonString(JObject obj, string name)
        {
            if (null != obj[name])
            {
                return obj[name].ToString();
            }
            return "";
        }
    }
}
